package net.pacman.controller;

import net.pacman.GamePanel;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

	private SurfaceHolder surface;
	private GamePanel panel;
	private boolean isRunning;

	public GameThread(SurfaceHolder surface, GamePanel panel) {

		super();

		this.surface = surface;
		this.panel = panel;
		
		this.isRunning = false;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Canvas canvas;

		while (isRunning) {
			canvas = null;
			Log.d("UPDATE THREAD", "DRAW UPGRADE");
			try {
				canvas = this.surface.lockCanvas();
				synchronized (surface) {

					this.panel.draw(canvas);
                                        this.panel.updateStates();
                                        
					Thread.sleep(100);
//                                        Log.d("Thread Sleep","sleeping");
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (canvas != null)
					surface.unlockCanvasAndPost(canvas);
			}
		}
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

}
